﻿#include "CustomDelegate.h"

CustomDelegate::CustomDelegate(QObject *parent)
    : QStyledItemDelegate(parent)
{

}

void CustomDelegate::registerFactory(int column, EditorFactory *factory)
{
    m_factories[column] = factory;
}

QWidget *CustomDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    if (m_factories.contains(index.column())) {
        return m_factories[index.column()]->createEditor(parent, index);
    }
    return QStyledItemDelegate::createEditor(parent, option, index);
}

void CustomDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    if (m_factories.contains(index.column())) {
        m_factories[index.column()]->setEditorData(editor, index);
    } else {
        QStyledItemDelegate::setEditorData(editor, index);
    }
}

void CustomDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
    if (m_factories.contains(index.column())) {
        m_factories[index.column()]->setModelData(editor, model, index);
    } else {
        QStyledItemDelegate::setModelData(editor, model, index);
    }
}
